package com.czk.webflux.controller;

import com.czk.webflux.entity.User;
import com.czk.webflux.servcie.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/**
 * 基于注解式webflux的响应式编程
 * 此时默认的容器是netty
 * springWebFlux+reactor+netty的模式
 * @Author:ChenZhangKun
 * @Date: 2020/11/26 21:13
 */
@RestController
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 通过id查询
     * @param id
     * @return
     */
    @GetMapping("/user/{id}")
    public Mono<User> getById(@PathVariable("id") Integer id){
        return userService.findUserById(id);
    }

    /**
     * 拿到全部
     * @return
     */
    @GetMapping("/user")
    public Flux<User> getAllUser(){
        return userService.findAllUser();
    }

    /**
     * 添加
     * @param user
     * @return
     */
    @PostMapping("/user/add")
    public Mono<Void> saveUser(User user){
        Mono<User> userMono=Mono.just(user);
        return userService.saveUserInfo(userMono);
    }
}
